package com.crazyread.imple;

import com.crazyread.comment.lang.Result;
import com.crazyread.dao.BooksMapper;
import com.crazyread.dao.BooksTypeMapper;
import com.crazyread.dao.BooksTypeMapper3;
import com.crazyread.dao.StatisticMapper;
import com.crazyread.model.domain.Book;
import com.crazyread.model.domain.BookChapter;
import com.crazyread.model.domain.BookType;
import com.crazyread.model.domain.BookType3;
import com.crazyread.service.BooksService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@Service
public class BooksServicelmlp implements BooksService {
    @Autowired
    BooksMapper booksMapper;
    @Autowired
    BooksTypeMapper booksTypeMapper;
    @Autowired
    StatisticMapper statisticMapper;
    @Autowired
    BooksTypeMapper3 booksTypeMapper3;
    @Override
    public List<Book> selectByBook() {
        return booksMapper.selectByBook();
    }

    @Override
    public Book selectByIdBook(int id) {
        Book book = booksMapper.selectBookById(id);
        String bookTypeName = booksTypeMapper.selectBookTypename(book.getBooktypeid());
        Integer comm_num = statisticMapper.getTotalCommNum(id);
        Integer hits  = statisticMapper.getTotalHit();
        book.setCommentsNum(comm_num);
        book.setBookTypeName(bookTypeName);
        book.setHits(hits);
        return book;
    }

    @Override
    public List<Book> selectByNameBook(String bookname) {
        List<Book> bookList = booksMapper.selectBookByName("%"+bookname+"%");
        for (int i = 0; i<bookList.size() ; i++){
            Book book = bookList.get(i);
            String bookTypeName = booksTypeMapper.selectBookTypename(book.getBooktypeid());
            book.setBookTypeName(bookTypeName);
        }
        return bookList;
    }

    @Override
    public PageInfo<BookChapter> selectBookWithPage(Integer page, Integer count) {
        PageHelper.startPage(page,count);
        List<BookChapter> bookChapters = booksMapper.selectByChapter();
        PageInfo<BookChapter> pageInfo = new PageInfo<>(bookChapters);
        return pageInfo;
    }

    @Override
    public List<BookChapter> selectByChapter() {
        return booksMapper.selectByChapter();
    }

    @Override
    public List<BookType> selectByType() {
        return booksTypeMapper.selectByType();
    }

    @Override
    public BookType selectByIdType(int id) {
        return booksMapper.selectByIdType(id);
    }

    @Override
    public List<BookChapter> selectByIdChapter(int id) {
        return booksMapper.selectByIdChapter(id);
    }

    @Override
    public Book updateByIdBook(Book book) {
        return booksMapper.updateByIdBook(book);
    }



    @Override
    public List<Book> selectRandom() {
        List<Book> bookList = booksMapper.selectRandom();
        for (int i= 0 ; i< bookList.size();i++){
            Book book1 = bookList.get(i);
            String bookTypeName = booksTypeMapper.selectBookTypename(book1.getBooktypeid());
            book1.setBookTypeName(bookTypeName);
        }
        return bookList;
    }

    @Override
    public BookType3 selectByIdTypeBook(int id) {
        return booksTypeMapper3.selectByIdTypeBook(id);
    }

    @Override
    public Result deleteByIdBook(int id) {
        return booksMapper.deleteByIdBook(id);
    }

    @Override
    public List<Book> selectByRankBook() {
        return booksMapper.selectByRankBook();
    }
    @Override
    public Book InsertByIdBookRank(int id) {
        return booksMapper.InsertByIdBookRank(id);
    }
}
